<template>
  <a-drawer
    title="调剂"
    :width="windowWidth"
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="CampusAdjustVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-alert :showIcon="false" message="请选择要调剂的院系（专业、方向）" banner />
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="姓名">
              <a-input placeholder="请输入..."  v-model="postParams._record.ksxm" disabled />
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" labelInValue v-decorator="['tjyxdm',{rules:[{required:true,message:'请选择院系'}]}]" @change='yxChange'>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" labelInValue v-decorator="['tjzydm',{rules: [{ required: true, message: '请选择专业!' }]}]" @change='zyChange'>
                <a-select-option v-for="i in selectMenu.zy" :key="i.zyh" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" labelInValue v-decorator="['yjfxdm',{rules:[{required:true,message:'请选择研究方向'}]},{initValue:''}]"  @change="yjfxChange">
                <a-select-option v-for="i in selectMenu.yjfx" :key="i.yjfxm" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="导师">
              <a-select :allowClear="true" showSearch optionFilterProp="children" labelInValue v-decorator="['tjdsygh',{initValue:''}]" >
                <a-select-option v-for="i in selectMenu.teachers" :key="i.id" :value="i.id">{{ i.gh+'-'+i.xm }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'
import { setTimeout } from 'timers';
import { constants } from 'fs';

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  props: {
    CampusAdjustVisible: {
      default: false
    },
    info: {
      default: () => ({})
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      windowWidth:"450",
      loading: false,
      postParams: {
        ksxxId:'',
        tjdsygh: '',
        tjdsyxm: '',
        tjyjfxdm: '',
        tjyjfxmc: '',
        tjyxdm: '',
        tjyxmc: '',
        tjzydm: '',
        tjzymc: '',
      },
      selectMenu: {
        yx: [] ,// 院系
        zy: [] ,//专业
        yjfx: [], //研究方向
        teachers:[] ,//获取导师
      },
      doctoralAdmissionYear: '',
    }
  },
  watch: {
    info (val) {
      this.postParams = {...this.postParams,...val}
      console.log(this.postParams)
      const {_record} = val;
      if(_record && _record.bkyxsm && _record.bkzydm) {
        this.form.getFieldDecorator('tjyxdm', {initialValue: {key: _record.bkyxsm, label: _record.bkyxsm + "-" + _record.bkyxsmc}})
        this.getProssion(_record.bkyxsm)
        this.form.getFieldDecorator('tjzydm', {initialValue: {key: _record.bkzydm, label: _record.bkzydm + "-" + _record.bkzymc}})
        this.getDirec(_record.bkzydm)
      }else{
        this.form.setFieldsValue({'tjzydm':''})
        this.form.setFieldsValue({'tjyxdm':''})
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  mounted(){
     this.windowWidth = document.body.clientWidth>650?650:document.body.clientWidth
  },
  beforeCreate() {
    this.form = this.$form.createForm(this)
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })

  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeNoAuth()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })

      this.$api.base.getSchoolYear().then(res=>{
        this.doctoralAdmissionYear = res.data.doctoralAdmissionYear;
      })
    },
    reset () {
      this.data = []
    },
    // 选择院系
    yxChange (code, option) {
      console.log(code)

      this.form.setFieldsValue({'tjzydm':''})
      this.form.setFieldsValue({'yjfxdm':''})
      this.form.setFieldsValue({'tjdsygh':''})

      this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'yjfx','')
      this.$set(this.selectMenu,'teachers','')
      if(code){
      this.getProssion(code.key)
      }
      // 获取导师
      // this.getTeachers(code.key)

    },
    // 获取专业
    getProssion(code){
      // 获取专业
      this.$api.base.majorAll({
        lsdwh: code
      }).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 选择专业
    zyChange(code){
      console.log(code)

      this.form.setFieldsValue({'yjfxdm':''})
      this.form.setFieldsValue({'tjdsygh':''})

      this.$set(this.selectMenu,'yjfx','')
      this.$set(this.selectMenu,'teachers','')
      if(code){
      this.getDirec(code.key)
      }
    },
    // 获取研究方向
    getDirec(code){
      this.$api.base.researchAll({
        xwccm: 3,
        zsxn: this.doctoralAdmissionYear,
        ssyxm: this.form.getFieldValue('tjyxdm').key,
        sszym: code
      }).then(res => {
        this.$set(this.selectMenu, 'yjfx', res.data)
      })
    },
    // 选择研究方向
    yjfxChange(code) {
      console.log(code)

      this.form.setFieldsValue({'tjdsygh':''})

      this.$set(this.selectMenu,'teachers','')
      if(code){
      this.getTeachers(code.key)
      }
    },
    // 获取导师
    getTeachers(code){
       if(code){
       this.$api.base.getGuide({
        zsxn: this.doctoralAdmissionYear,
        ssyxm: this.form.getFieldValue('tjyxdm').key,
        zym: this.form.getFieldValue('tjzydm').key,
        yjfxm: code
      }).then(res => {
          this.$set(this.selectMenu,'teachers',res.data)
      })
       }
    },
    // 选择招生年度
    zsndChange () {
    },
    // 关闭
    onClose () {
      this.reset()
       this.form.resetFields()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
      this.reset()
      // this.$emit('success', this.postParams)
      this.form.validateFields( (err,values) => {
        console.log('values:',values,err)
        if(err){
          this.$message.error("请选择必填项")
        }else{
          let tjyxmc,tjzymc,tjdsyxm
          if(this.form.getFieldsValue().tjyxdm.label){
            tjyxmc = this.form.getFieldsValue().tjyxdm.label.split("-")[1]
          }
          if(this.form.getFieldsValue().tjzydm.label) {
            tjzymc = this.form.getFieldsValue().tjzydm.label.split("-")[1]
          }
          if(this.form.getFieldsValue().tjdsygh.label){
            tjdsyxm = this.form.getFieldsValue().tjdsygh.label.split("-")[1]
          }
          var params0 = {
            ksxxId:this.postParams.ksxxId,
            tjdsygh: this.form.getFieldsValue().tjdsygh.key,
            tjdsyxm: tjdsyxm,
            tjyjfxdm: this.form.getFieldsValue().yjfxdm.key,
            tjyjfxmc: this.form.getFieldsValue().yjfxdm.label,
            tjyxdm: this.form.getFieldsValue().tjyxdm.key,
            tjyxmc: tjyxmc,
            tjzydm: this.form.getFieldsValue().tjzydm.key,
            tjzymc: tjzymc,
          }

          var params = Object.assign({},this.postParams,params0)
          delete params.createDate  // 删除时间 不然会报错
          delete params.updateDate

          console.log(params)
          this.$api.doctorRecruit.campusAdjustAdd(params0).then(res => {
            this.reset()
            console.log(res)
            this.$emit('success')
             this.form.resetFields()
          }).finally(() => {
            this.loadingBtn = false
          }).catch(function(reason) {
            console.log('catch:', reason);
          });
        }
      })

    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
